package co.fitstart.mobile.web.controller.profile;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.BusinessException;
import com.demo2do.core.support.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.wechat.web.interceptor.WechatJsConfig;

import co.fitstart.entity.coupon.Coupon;
import co.fitstart.entity.user.User;
import co.fitstart.entity.user.profile.UserProfile;
import co.fitstart.mobile.service.CouponService;
import co.fitstart.mobile.service.UserService;
import co.fitstart.mobile.web.interceptor.MobileDetect;
import co.fitstart.mobile.web.interceptor.Router;
import co.fitstart.mobile.web.interceptor.WechatAuthority;
import co.fitstart.mobile.web.resolver.Secure;

/**
 * 
 * @author mohanlan
 *
 */
@Controller
@RequestMapping("/profile")
public class ProfileEditController {
    
    private static final Logger logger = LoggerFactory.getLogger(ProfileEditController.class);
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private CouponService couponService;
    
    /**
     * profile info index page
     * 
     * @param user
     * @return
     */
    @RequestMapping("/info")
    @Router
    @WechatAuthority
    @MobileDetect
    @WechatJsConfig
    public ModelAndView info(@Secure() User user) {
        
        ModelAndView modelAndView = new ModelAndView("profile/profile-info");
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
    
    /**
     * edit region
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/region", method = RequestMethod.GET)
    @Router
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String region(@Secure User user) {
        
        Result result = new Result();
        
        try {
            
            UserProfile userProfile = user.getProfile();
            userProfile = userProfile == null ? new UserProfile() : userProfile;
            
            result.success()
                  .data(userProfile.describe())
                  .message("请求我的地区成功");
               
        } catch (Exception e) {
            
            logger.error("region() error", e);
            result.fail().message("请求我的地区失败");
        
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * on edit region
     * 
     * @param user
     * @param country
     * @param prd --- province region district
     * @return
     */
    @RequestMapping(value = "/region", method = RequestMethod.PATCH)
    @Router
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String onEditRegion(@Secure User user, 
                                             @RequestParam("country") String country,
                                             @RequestParam("prd") String prd) {
        
        Result result = new Result();
        
        try {
            
            userService.editRegion(user, country, prd);
            result.success().message("修改我的地区成功");
            
        } catch (Exception e) {
            logger.error("onEditRegion() error", e);
            result.fail().message("修改我的地区失败");
            
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * edit address
     * 
     * @param user
     * @return
     */
    @RequestMapping(value = "/address", method = RequestMethod.GET)
    @Router
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String address(@Secure User user) {
        
        Result result = new Result();
        
        try {
            
            if(user.getProfile() != null) {
                result.data("address", user.getProfile().getAddress());
            }
            
            result.success().message("加载收货地址成功");
            
        } catch (Exception e) {
            
            result.fail().message("加载收货地址失败");
        }
        
        return JsonUtils.toJsonString(result);
    } 
    
    /**
     * on edit address
     * 
     * @param user
     * @param address
     * @return
     */
    @RequestMapping(value = "/address", method = RequestMethod.PATCH)
    @Router
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String onEditAddress(@Secure User user,
                                              @RequestParam("address") String address) {
        Result result = new Result();
        
        try {
            
            userService.editAddress(user, address);
            result.success().message("修改收货地址成功");
            
        } catch (Exception e) {
            
            logger.error("onEditAddress() error", e);
            result.fail().message("修改收货地址失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
    
    /**
     * online year year coupon give
     * @param user
     * @param couponId
     * @param mobile
     * @return
     */
    @RequestMapping(value = "/coupon/{couponId}/give", method = RequestMethod.PATCH)
    @Router
    @WechatAuthority
    @MobileDetect
    public @ResponseBody String onGiveCoupon(@Secure User user,
                                             @PathVariable("couponId") Long couponId,
                                             @RequestParam("mobile") String mobile) {
        Result result = new Result();
        
        try {
            Coupon coupon = couponService.load(Coupon.class, couponId);
            User to = userService.getUser(mobile);
            
            couponService.give(coupon, user, to);
            result.success().message("转赠成功");
        } catch (BusinessException e) {
            result.fail().message(e.getMessage());
        } catch (Exception e) {
            logger.error("onGiveCoupon() error", e);
            result.fail().message("转赠失败");
        }
        
        return JsonUtils.toJsonString(result);
    }
}
